package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.admin.request.AirdropGrantRecordExportRequest;
import com.sc.nft.admin.request.TempRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.TempAirdropRecord;
import com.sc.nft.entity.vo.AirdropGrantRecordExportListVO;
import com.sc.nft.service.TempAirdropRecordService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-07-27
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/temp/tempAirdropRecord")
@Api(tags="空投明细记录导出模块")
public class TempAirdropRecordController {



    private final TempAirdropRecordService tempAirdropRecordService;



    @PostMapping(value = "/collectionExport", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "空投明细记录导出", notes = "空投明细记录导出", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result tempAirdropRecordExcel(@Valid @RequestBody AirdropGrantRecordExportRequest request){
        if (StrUtil.isBlank(request.getCollectionName()) && ObjectUtil.isNull(request.getStartTime()) && ObjectUtil.isNull(request.getEndTime())){
            Result.fail("藏品名称筛选和时间筛选不能都为空");
        }
        return Result.success(tempAirdropRecordService.saveTempAirdropRecord(request.getStartTime(),request.getEndTime(),request.getCollectionName()));
    }


    @PostMapping(value = "/download", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "空投明细记录导出下载", notes = "空投明细记录导出下载", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<AirdropGrantRecordExportListVO>> findTempAirdropRecordList(@Valid @RequestBody TempRequest request){
        List<TempAirdropRecord> tempAirdropRecordList = tempAirdropRecordService.findTempAirdropRecordList(request.getExportRecordsId());
        List<AirdropGrantRecordExportListVO> airdropGrantRecordExportVOS = BeanUtil.copyToList(tempAirdropRecordList, AirdropGrantRecordExportListVO.class);
        return Result.success(airdropGrantRecordExportVOS);
    }

}
